﻿using Lextm.SharpSnmpLib;
using Models.Entities;
using Services.Equ.PcapCommunication.Abstract;
using SharpPcap;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static Dm.parser.LVal;

namespace Services.Equ.PcapCommunication
{
    /// <summary>
    /// 搜索设备通讯
    /// </summary>
    public class SearchDeviceCommunication : AbstractPcapCommunication<EquInfo>
    {
        public SearchDeviceCommunication(ILiveDevice device):base(PhysicalAddress.Parse("FF-FF-FF-FF-FF-FF"),"",device)
        {

        }
        public SearchDeviceCommunication(PhysicalAddress destinationMac, string password, ILiveDevice device) : base(destinationMac, password, device)
        {
        }
        public override bool IsResponse(byte[] bytes)
        {
            return bytes[16] == 0x05 && bytes[17] == 0x01;
        }
        public override EquInfo ConvertData2Result(byte[] bytes)
        {
            var bodyData = bytes[26..^1];
            EquInfo equInfo = new EquInfo();
            int index = 0;
            while (index < bodyData.Length)
            {
                int dataLength = bodyData[index + 3];
                int dataType = bodyData[index + 1];
                dataLength += 4;
                int skipLength = dataLength;
                if (dataLength % 2 != 0)
                {
                    skipLength = dataLength + 1;
                }
                var body = bodyData[(index + 4)..(dataLength + index >= bodyData.Length - 1 ? bodyData.Length - 1 : dataLength + index)];
                SetValue(equInfo, dataType, body);
                index += skipLength;
            }
            return equInfo;
        }

        public override byte[] GetPayload()
        {
            return [0xFE, 0xFE, 0x05, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x04, 0xff, 0xff, 0x00, 0x00];
        }
        public void Init(byte[] reciveData)
        {
        }
        private void SetValue(EquInfo equInfo,int type, byte[] data)
        {
            switch (type)
            {
                case 1:
                    equInfo.BootVersion = Encoding.UTF8.GetString(data);
                    break;
                case 2:
                    equInfo.KennelVersion = Encoding.UTF8.GetString(data);
                    break;
                case 3:
                    equInfo.ImageVersion = Encoding.UTF8.GetString(data);
                    break;
                case 4:
                    equInfo.HardwareVersion = Encoding.UTF8.GetString(data);
                    break;
                case 5:
                    equInfo.VendorVersion = Encoding.UTF8.GetString(data);
                    break;
                case 6:
                    equInfo.ProductLine = Encoding.UTF8.GetString(data);
                    break;
                case 7:
                    equInfo.TypeId = Encoding.UTF8.GetString(data);
                    break;
                case 8:
                    equInfo.Oid = Encoding.UTF8.GetString(data);
                    break;
                case 9:
                    equInfo.SN = Encoding.UTF8.GetString(data);
                    break;
                case 10:
                    equInfo.BootVersion = Encoding.UTF8.GetString(data);
                    break;
                //mac地址
                case 11:
                    equInfo.Mac = BitConverter.ToString(data);
                    break;
                case 14:
                    equInfo.Ip = $"{data[0]}.{data[1]}.{data[2]}.{data[3]}";
                    equInfo.Mask = $"{data[4]}.{data[5]}.{data[6]}.{data[7]}";
                    equInfo.GateWay = $"{data[8]}.{data[9]}.{data[10]}.{data[11]}";
                    equInfo.Vlan = data[15];
                    //IpRole = $"{data[19]}";
                    break;
                default:
                    break;
            }
        }

        public override byte[] CreateBlockData()
        {
            return Array.Empty<byte>();
        }
    }
}
